<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2017/11/9  19:27
 */
namespace app\api\logic;
use app\api\BaseModel;
use app\api\model\Member as memberModel;

class Circle extends BaseModel{

	//检测参数是否已传
    protected $ParameterDetection  = [
		'addCircle' => ['type','circle_name','remarks','province','city','county','street','address'],
		'addCircleGoods' => ['cid','name','des','sort','price'],
		'addCircleMood' => ['cid','des'],
		'addCircleMoodImg' => ['cid'],
		'circleGoodsInfo' => ['goods_id','circle_type'],
		'goodsReview' => ['goods_id','cid'],
		'goodsReviewDel' => ['id'],
		'circleSelect'=>['uid','search','type'],
		'circleInfo'=>['cid'],
		'attention'=>['uid','action'],
		'circleLike'=>['cid'],
		'circleCommentAdd'=>['comment_id','cid','content'],
		'circleCommentDel'=>['id'],
		'circleKeep'=>['cid','action'],
		'circleGoodsKeep'=>['cid','goods_id','action'],
		'addCircleimg'=>['cid'],
		'delCircleimg'=>['cid','image_path'],
		'deleteCircle'=>['cid'],
		'editGoodsImg'=>['goods_id','circle_type','type'],
		'delGoodsImg'=>['goods_id','circle_type','img_path']
    ];


	/**
	* 需求圈查询
	*/
	public function circleSelect(){
		$uid = $this->requestData['uid'];
		$page = isset($this->requestData['page'])?$this->requestData['page']:'1';
		$type = $this->requestData['type'];
		$search = $this->requestData['search'];
		$search_type = isset($this->requestData['search_type'])?$this->requestData['search_type']:'';
		$where = [];
		$where['is_del'] = 1;
		$whereor = [];

		if(!empty($uid)){
			$member_result = db('member')->where(['id'=>$uid])->find();
			$where['user_id'] = $uid;
		}

		if(!empty($type)){
			$type_array = explode(",", $type);
			$type = ['in',$type_array];

			$where['type']=$type;
		}

		if(!empty($search)){
			$where['goods_search']=['like',"%$search%"];
			$whereor['circle_name']=['like',"%$search%"];
		}

		if($search_type == '1'){
			//查询收藏的需求圈
			$circle_keep_where = [];
			if(!isset($where['user_id'])){
				$circle_keep_where['user_id'] = $this->user['id'];
			}else{
				$circle_keep_where['user_id'] = $where['user_id'];
			}
			$circle_keep_cid = db('circle_keep')->where($circle_keep_where)->field('cid')->select();
			$circle_keep_cid = array_column($circle_keep_cid, 'cid');
			$where['id'] = ['in',$circle_keep_cid];
			unset($where['user_id']);
		}


		$result = db('circle')->where($where)->whereor($whereor)->page($page,'10')->order('id desc')->select();
		$cids = array_column($result, 'id');
		$type = array_column($result, 'type');

		foreach ($cids as $key => $value) {
			if($type[$key] == '1'){
				$circle_goods = db('circle_goods');
			}

			if($type[$key] == '2'){
				$circle_goods = db('circle_goods_demand');
			}
			
			if($type[$key] == '3'){
				//心情圈
				$circle_goods = db('circle_goods_mood');
			}

			$circle_goods = $circle_goods->where(['cid'=>$value])->order('sort desc')->select();
			$result[$key]['circle_image'] = IMG_PATH.$result[$key]['circle_image'];
			$result[$key]['time_recording'] = getStrdiftime($result[$key]['time']);
			$member_result = db('member')->where(['id'=>$result[$key]['user_id']])->field('nickname,headimage,hx_username,hx_passwd,im_username')->find();
			$result[$key]['mem_avatar'] = $member_result['headimage'];
			$result[$key]['mem_name'] = $member_result['nickname'];
			$result[$key]['hx_username'] = $member_result['hx_username'];
			$result[$key]['hx_passwd'] = $member_result['hx_passwd'];
			$result[$key]['im_username'] = $member_result['im_username'];
			if(!empty($circle_goods)){
				foreach ($circle_goods as $goods_key => $goods_value) {
					if(in_array($type[$key], ['1','2'])){
						$goods_value['image'] = IMG_PATH.$goods_value['image'];
					}
					
					if($type[$key] == '1'){
						$goods_value['price'] = $goods_value['price']/100;   //数据库已分为单位
					}

					//心情全显示
					if(!empty($goods_value['images'])){
						$goods_value['images'] = json_decode($goods_value['images'],1);
						foreach ($goods_value['images'] as $k => &$v) {
							$v = IMG_PATH.$v;
						}
					}

					$result[$key]['goods_result'][] = $goods_value;
				}
			}else{
				$result[$key]['goods_result']=[];
			}

			$circle_comment_result = db('circle_comment')->where(['cid'=>$value])->select();

			foreach ($circle_comment_result as $ccr => &$ccrv) {
				unset($ccrv['thumb_num']);
				if(!empty($ccrv['comment_id'])){
					$mem_name_result = db('circle_comment')->where(['id'=>$ccrv['comment_id']])->field('mem_name')->find();
					$circle_comment_result[$ccr]['reply_name'] = $mem_name_result['mem_name'];
				}
			}

			$result[$key]['circle_comment'] = $circle_comment_result;

			$iskeepwhere['cid']=$value;
			$iskeepwhere['user_id'] = $this->user['id'];

			//是否收藏
			$iskeep = db('circle_keep')->where($iskeepwhere)->find();
			if($iskeep){
				$result[$key]['is_keep'] = 1;
			}else{
				$result[$key]['is_keep'] = 0;
			}

			$iszan = db('circle_thumb_record')->where($iskeepwhere)->find();
			if($iszan){
				$result[$key]['is_zan'] = 1;
			}else{
				$result[$key]['is_zan'] = 0;
			}

			//是否关注
			$isattention = db('attention')->where(['attention_user_id'=>$result[$key]['user_id'],'user_id'=>$this->user['id']])->find();
			if(!empty($isattention)){
				$result[$key]['is_attent'] = 1;
			}else{
				$result[$key]['is_attent'] = 0;
			}

			$hx = db('member')->where('id='.$result[$key]['user_id'])->field('hx_username,hx_passwd')->find();
			$result[$key]['hx_username'] = $hx['hx_username'];
			$result[$key]['hx_passwd'] = $hx['hx_passwd'];
		}
		return $result;
	}

	/**
	* 圈详情
	*/
	public function circleInfo(){
		$cid = $this->requestData['cid'];

		$result = db('circle')->where(['id'=>$cid])->find();
		if(empty($result))return ['message'=>'cid为非法id!'];

		//查询用户与头像
		$member_result = db('member')->where(['id'=>$result['user_id']])->field('nickname,headimage,hx_username,hx_passwd,im_username')->find();

		$result['mem_avatar'] = $member_result['headimage'];
		$result['mem_name'] = $member_result['nickname'];
		$result['hx_username'] = $member_result['hx_username'];
		$result['hx_passwd'] = $member_result['hx_passwd'];
		$result['im_username'] = $member_result['im_username'];

		$iskeepwhere['cid']=$cid;
		$iskeepwhere['user_id'] = $this->user['id'];
		//是否收藏
		$iskeep = db('circle_keep')->where($iskeepwhere)->find();
		if($iskeep){
			$result['is_keep'] = 1;
		}else{
			$result['is_keep'] = 0;
		}
		//是否点赞
		$iszan = db('circle_thumb_record')->where($iskeepwhere)->find();
		if($iszan){
			$result['is_zan'] = 1;
		}else{
			$result['is_zan'] = 0;
		}
		//是否关注
		$isattention = db('attention')->where(['attention_user_id'=>$result['user_id'],'user_id'=>$this->user['id']])->find();
		if(!empty($isattention)){
			$result['is_attent'] = 1;
		}else{
			$result['is_attent'] = 0;
		}

		if($result['type'] == '1'){
			$circle_goods = db('circle_goods');
		}

		if($result['type'] == '2'){
			$circle_goods = db('circle_goods_demand');
		}

		if($result['type'] == '3'){
			$circle_goods = db('circle_goods_mood');
		}


		$circle_goods_result = $circle_goods->where(['cid'=>$cid])->order('sort desc')->select();
		
		if(!empty($circle_goods_result)){
			foreach ($circle_goods_result as $key => &$value) {
				if(in_array($result['type'], ['1','2'])){
					$value['image'] = IMG_PATH.$value['image'];
				}

				//买手圈
				if($result['type'] == '1'){
					$value['price'] = $value['price']/100;
				}

				$circle_goods_keep = db('circle_goods_keep')->where(['user_id'=>$this->user['id'],'cid'=>$cid,'goods_id'=>$value['id']])->find();

				if(!empty($value['images'])){
					$value['images'] = json_decode($value['images'],1);

					foreach ($value['images'] as $ka => &$va) {
						$va = IMG_PATH.$va;
					}
				}

				if(!empty($circle_goods_keep)){
					$value['is_goods_keep'] = '1';
				}else{
					$value['is_goods_keep'] = '0';
				}
			}

			$result['goods_result'] = $circle_goods_result;
		}else{
			$result['goods_result'] = [];
		}
		$img = @unserialize($result['circle_image']);
		if(!empty($img)){
			foreach ($img as $k=>$val){
				$img[$k] = IMG_PATH.$img[$k];
			}
			$result['circle_image'] = $img;
		}else{
			$result['circle_image'] = [];
		}

		$circle_comment_result = db('circle_comment')->where(['cid'=>$cid])->select();

		if(!empty($circle_comment_result)){
			foreach ($circle_comment_result as $ccr => $ccrv) {
				if(!empty($ccrv['comment_id'])){
					$mem_name_result = db('circle_comment')->where(['id'=>$ccrv['comment_id']])->field('mem_name')->find();
					$circle_comment_result[$ccr]['reply_name'] = $mem_name_result['mem_name'];
				}
			}

			$result['circle_comment'] = $circle_comment_result;
		}else{
			$result['circle_comment'] = [];
		}

		//检测是否可以删除圈评论
		if($result['user_id'] != $this->user['id']){
			$result['circle_comment_del'] = '0'; 
		}else{
			$result['circle_comment_del'] = '1';
		}

		return $result;
	}

	/**
	 * 删除需求圈
	 */
	public function deleteCircle()
	{
		$cid = $this->requestData['cid'];
		$uid = $this->user['id'];
		$codition['id'] = $cid;
		$circle = db('circle')->where($codition)->find();
		if(!$circle)return ['message'=>'参数为空！'];
		if($circle['user_id'] != $uid)return ['message'=>'无权限'];
		$data['is_del']=-1;
		$res = db('circle')->where($codition)->update($data);
		if(!$res)return ['message'=>'删除失败'];
	}

	/**
	* 圈评论添加
	*/
	public function circleCommentAdd(){
		$cid = $this->requestData['cid'];
		$comment_id = $this->requestData['comment_id'];
		$content = $this->requestData['content'];

		$result = db('circle')->where(['id'=>$cid])->find();
		if(empty($result))return ['message'=>'cid为非法id!'];

		$data['cid'] = $cid;
		if(!empty($comment_id)){
			if($result['user_id'] != $this->user['id'])return ['message'=>'你没有权限回复评论!'];
			$comment_uses_info = db('circle_comment')->where(['id'=>$comment_id])->find();
			if(empty($comment_uses_info))return ['message'=>'comment_id参数错误!'];
			$data['comment_id']=$comment_id;
		}
		$data['content'] = $content;
		$data['create_time'] = time();
		$data['user_id'] = $this->user['id'];
		$userInfo = (new memberModel())->getUserInfo($this->user['id']);
		$data['mem_avatar'] = $userInfo['headimage'];
		$data['mem_name'] = $userInfo['nickname'];

		$circle_comment = db('circle_comment')->insert($data);
		if(!$circle_comment)['message'=>'添加评论失败!'];
	}

	/**
	*	圈评论删除
	*/
	public function circleCommentDel(){
		$id = $this->requestData['id'];
		$user_id = db('circle_comment')->alias('cc')->join('circle c','c.id=cc.cid')->where(['cc.id'=>$id])->value('c.user_id');
		if(empty($user_id))return ['message'=>'id非法参数!'];
		if($this->user['id'] != $user_id)return ['message'=>'没有权限删除!'];

		$result = db('circle_comment')->where(['id'=>$id])->delete();
		if(!$result)return ['message'=>'删除失败'];
	}

	/**
	* 圈点赞
	*/
	public function circleLike(){
		$cid = $this->requestData['cid'];
		$circle_thumb_result = db('circle_thumb_record')->where(['cid'=>$cid,'user_id'=>$this->user['id']])->find();
		if(!empty($circle_thumb_result))return ['message'=>'已点赞过了!'];

		$circle_result = db('circle')->where(['id'=>$cid])->find();

		if(empty($circle_result))return ['message'=>'cid是非法参数!'];

		$data['user_id']=$this->user['id'];
		$data['cid']=$cid;
		$result = db('circle_thumb_record')->insert($data);

		if(!$result)return ['message'=>'点赞失败!'];

		db('circle')->where(['id'=>$cid])->setInc('thumb_num');

	}

	/**
	* 关注/取消关注
	*/
	public function attention(){
		$attention_uid = $this->requestData['uid'];
		$attention_action = trim($this->requestData['action']);
		$uid = $this->user['id'];

		if(empty($attention_action))return ['message'=>'参数错误！'];
		if(empty($attention_uid))return ['message'=>'参数为空!'];

		if($attention_action == 'add'){
			if($attention_uid == $uid)return ['message'=>'不能关注自己!'];
			$attention_result = db('attention')->where(['user_id'=>$uid,'attention_user_id'=>$attention_uid])->find();
			if(!empty($attention_result))return ['message'=>'已关注过了!'];
			$data['user_id']=$uid;
			$data['attention_user_id']=$attention_uid;
			$data['creatime']=time();
			$res = db('attention')->insert($data);
		}
		if($attention_action == 'del'){
			$codition['user_id'] = $uid;
			$codition['attention_user_id'] = $attention_uid;
			$attention = db('attention')->where($codition)->find();
			if(!$attention)return ['message'=>'数据为空!'];
			$res =db('attention')->where($codition)->delete();
		}
		if(!$res)return ['message'=>'操作失败'];
	}

	//关注列表
	public function attentionList()
	{
		$where['user_id'] = $this->user['id'];
		$page = isset($this->requestData['page'])?$this->requestData['page']:'1';
		$result = db('attention')->where($where)->page($page,'10')->select();
		foreach($result as $k=>$val){
			$member = db('member')->where('id='.$result[$k]['attention_user_id'])->field('nickname,headimage,is_buyer')->find();
			$result[$k]['attention_user_name']=$member['nickname'];
			$result[$k]['attention_user_headimage']=$member['headimage'];
			if($member['is_buyer'] == '1'){
				//是买手
				$result[$k]['type'] = '1';
			}else{
				$result[$k]['type'] = '2';
			}
		}
		return $result;
	}

	/**
	*  收藏
	*/
	public function circleKeep(){
		$cid = $this->requestData['cid'];
		$uid = $this->user['id'];
		$attention_action = trim($this->requestData['action']);
		if(empty($attention_action))return ['message'=>'参数错误！'];
		$circle_result = db('circle')->where(['id'=>$cid])->find();
		if(empty($circle_result))return ['message'=>'cid是非法参数!'];
		$where['user_id'] = $uid;
		$where['cid']=$cid;
		$circle_keep = db('circle_keep')->where($where)->find();
		if($attention_action == 'add'){
			if(!empty($circle_keep))return ['message'=>'已收藏过了!'];
			$data['cid'] = $cid;
			$data['user_id'] = $this->user['id'];
			$result = db('circle_keep')->insert($data);
			if(!$result)return ['message'=>'收藏失败!'];
		}
		if($attention_action == 'del'){
			if(empty($circle_keep))return ['message'=>'数据为空！'];
			$res = db('circle_keep')->where($where)->delete();
			if(!$res)return ['message'=>'取消收藏失败'];
		}
	}

	/**
	 *  收藏
	 */
	public function circleKeepList(){
		$where['ck.user_id']=$this->user['id'];
		$page = isset($this->requestData['page'])?$this->requestData['page']:1;
		$result = db('circle_keep')->alias('ck')->join('circle c', 'c.id = ck.cid', 'INNER')->where($where)->field('ck.*,c.circle_name')->page($page,'10')->select();
		foreach ($result as $key => $value) {
			$circle_goods = db('circle_goods')->where(['cid'=>$value['cid']])->order('id desc')->find();
			$circle_goods = IMG_PATH.$circle_goods['image'];
			$result[$key]['circle_goods'] = $circle_goods;
		}
		return $result;
	}

	/**
	*	商品收藏
	*/
	public function circleGoodsKeep(){
		$cid = $this->requestData['cid'];
		$goods_id = $this->requestData['goods_id'];
		$action = $this->requestData['action'];

		if(!in_array($action,['add','det']))return ['message'=>'action参数有误!'];

		$circle = db('circle')->where(['id'=>$cid])->find();	
		if(empty($circle))return ['message'=>'cid参数有误'];

		if($circle['type'] == '1'){
			$circle_goods = db('circle_goods')->where(['id'=>$goods_id,'cid'=>$cid])->find();
		}else{
			$circle_goods = db('circle_goods_demand')->where(['id'=>$goods_id,'cid'=>$cid])->find();
		}

		if(empty($circle_goods))return ['message'=>'goods_id参数有误!'];

		if($action == 'add'){
			$circle_goods_keep = db('circle_goods_keep')->where(['user_id'=>$this->user['id'],'goods_id'=>$goods_id,'cid'=>$cid])->find();
			if(!empty($circle_goods_keep))return ['message'=>'已收藏过了!'];
			$result = db('circle_goods_keep')->insert(['user_id'=>$this->user['id'],'goods_id'=>$goods_id,'cid'=>$cid]);
		}

		if($action == 'det'){
			$result = db('circle_goods_keep')->where(['user_id'=>$this->user['id'],'goods_id'=>$goods_id,'cid'=>$cid])->delete();
		}
		if(!$result)return ['message'=>'收藏失败'];
	}

	/**
	*	商品收藏列表
	*/
	public function goodsKeepList(){
		$page = isset($this->requestData['page'])?$this->requestData['page']:1;
		$result = [];

		$circle_goods_keep = db('circle_goods_keep')->where(['user_id'=>$this->user['id']])->page($page,'10')->select();

		foreach ($circle_goods_keep as $key => $value) {
			$circle = db('circle')->where(['id'=>$value['cid']])->find();

			if($circle['type'] == '1'){
				$circle_goods = db('circle_goods');
			}

			if($circle['type'] == '2'){
				$circle_goods = db('circle_goods_demand');
			}

			$goods = $circle_goods->where(['id'=>$value['goods_id']])->find();
			if(!empty($goods['image'])){
				$goods['image'] = IMG_PATH.$goods['image'];
			}
			
			$goods['images'] = @json_decode($goods['images'],1);
			if(!empty($goods['images'])){ 
				foreach($goods['images'] as &$v){
					$v = IMG_PATH.$v;
				}
			}
			if($circle['type'] == '1'){
				$goods['price'] = $goods['price']/100;
			}


			$result[$key]['cid'] = 	$value['cid'];
			$result[$key]['goods'] = $goods;		
			$result[$key]['type'] = $circle['type'];		
		}

		return $result;
	}

    /**
     * 添加需求圈
     */
    public function addCircle(){
    	$data=[];
		$data['type']=$this->requestData['type'];
		$data['circle_name']=$this->requestData['circle_name'];
		$data['remarks']=$this->requestData['remarks'];
		$data['province']=$this->requestData['province'];
		$data['city']=$this->requestData['city'];
		$data['county']=$this->requestData['county'];
		$data['street']=$this->requestData['street'];
		$data['address']=$this->requestData['address'];
		$data['user_id']=$this->user['id'];
		$data['time']=time();

		if(!in_array($data['type'],['1','2','3']))return ['message'=>'参数不合法!'];

		$userInfo = (new memberModel())->getUserInfo($this->user['id']);

		if($data['type'] == '1' || $data['type'] == '3'){
			if($userInfo['is_buyer'] == '0')return ['message'=>'你不是买手!'];
		}

		$circle = db('circle')->insert($data);
		$result['cid'] = db('circle')->getLastInsID();
		$result['type'] = $data['type'];

		return $result;
    }

	/**
	 * 添加需求圈图片
	 */
	public function addCircleimg()
	{
		$where['id']=$this->requestData['cid'];
		$uid = $this->user['id'];
		$circle = db('circle')->where($where)->find();
		if($circle['user_id'] != $uid)return ['message'=>'无权限'];
		$files = request()->file('circle_image');
		if(!$files)return ['message'=>'请添加图片！'];
		foreach($files as $file){
			// 移动到框架应用根目录/public/uploads/ 目录下
			$info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public/static/uploads');
			if($info){
				$imgs[] = '/'.$info->getSaveName();
			}else{
				// 上传失败获取错误信息
				return ['message'=>$file->getError()];
			}
		}
		$data['circle_image'] = serialize($imgs);
		$circle = db('circle')->where($where)->find();

		db('circle')->where($where)->update($data);
	}

	//删除需求圈图片
	public function delCircleimg()
	{
		$where['id']=$this->requestData['cid'];

		$image_path = $this->requestData['image_path'];

		if(empty($image_path))return ['message'=>'传删除图片路径!'];

		$uid = $this->user['id'];

		$circle = db('circle')->where($where)->find();
		if($circle['user_id'] != $uid)return ['message'=>'无权限'];

		$img = unserialize($circle['circle_image']);

		$key = array_search($image_path, $img);

		if(!is_numeric($key))return ['message'=>'image_path参数有误!'];

		unset($img[$key]);

		$data['circle_image'] = serialize($img);
		
		db('circle')->where($where)->update($data);
	}

    /**
    * 需求圈商品需求添加
    */
    public function addCircleGoods(){
    	$where['id']=$this->requestData['cid'];

    	$circle = db('circle')->where($where)->find();
    	if(empty($circle))return ['message'=>'参数不合法'];
    	$goods_id = isset($this->requestData['goods_id'])?$this->requestData['goods_id']:'';

    	$data['adress']=isset($this->requestData['adress'])?$this->requestData['adress']:'';
    	$data['name']=$this->requestData['name'];
    	$data['cid']=$this->requestData['cid'];
		$data['des']=$this->requestData['des'];
		$data['sort']=$this->requestData['sort'];
		$data['stock']=isset($this->requestData['stock'])?$this->requestData['stock']:'0';
		
		if($circle['type'] == '1'){
			//买手圈
			$strlen_price = strlen($this->requestData['price']);
			if($strlen_price > 10)return ['message'=>'金额过大!'];
			$data['price']=$this->requestData['price']*100;
			$data['description']=isset($this->requestData['description'])?$this->requestData['description']:'';
			$data['remarks']=isset($this->requestData['remarks'])?$this->requestData['remarks']:'';
			$data['freight']=isset($this->requestData['freight'])?$this->requestData['freight']:'';
			$circle_goods_db = db('circle_goods');
		}

		if($circle['type'] == '2'){
			//需求圈
			$data['price']=isset($this->requestData['price'])?$this->requestData['price']:'';
			$data['creatime']=isset($this->requestData['creatime'])?$this->requestData['creatime']:'';
			$circle_goods_db = db('circle_goods_demand');
		}

		//更新需求圈便于搜索
		$circle_goods_result = $circle_goods_db->where(['cid'=>$this->requestData['cid']])->select();
		$goods_search = array_column($circle_goods_result, 'name');
		$goods_search[] = $this->requestData['name'];
		$goods_search = implode(',', $goods_search);
		if($circle['type'] == '1'){
			$goods_search2 = array_column($circle_goods_result, 'description');
			$description = isset($this->requestData['description'])?$this->requestData['description']:'';
			$goods_search2[] = $description;
			if(!empty($description)){
				$goods_search2 = implode(',', $goods_search2);
				$goods_search .=','.$goods_search2;
			}
		}
		db('circle')->where(['id'=>$this->requestData['cid']])->update(['goods_search'=>$goods_search]);

        if(empty($goods_id)){
        	$circle_goods = $circle_goods_db->insertGetId($data);
    	}else{
    		foreach ($data as $key => $value) {
    			if(empty($value)){
    				unset($data[$key]);
    			}
    		}
    		$circle_goods = $circle_goods_db->where(['id'=>$goods_id])->update($data);
    		if($circle_goods){
    			$circle_goods = $goods_id;
    		}
    	}
        if(!$circle_goods)return ['message'=>'添加失败'];
        $result['goods_id'] = $circle_goods;
        return $result;
    }

    /**
    *	需求圈心情需求添加
    */
    public function addCircleMood(){
    	$where['id']=$this->requestData['cid'];

    	$circle = db('circle')->where($where)->find();
    	if(empty($circle))return ['message'=>'参数不合法'];

    	$des = $this->requestData['des'];
    	$cid = $this->requestData['cid'];
    	
    	$data['des'] = $des;
    	$data['cid'] = $cid;
    	$data['creatime'] = time();

    	$result = db('circle_goods_mood')->insert($data);
    	if(!$result)return ['message'=>'添加失败'];
    }

    /**
    *	心情圈图片添加
    */
    public function addCircleMoodImg(){
    	$cid = $this->requestData['cid'];
    	$circle_goods_mood = db('circle_goods_mood')->where(['cid'=>$cid])->find();
    	if(empty($circle_goods_mood))return ['message'=>'参数不合法'];

    	$file = request()->file('image');
    	$data = [];

    	if($file){
    		//上传多图
    		$images = [];
    		if(!empty($circle_goods_mood['images'])){
    			$images = json_decode($circle_goods_mood['images'],1);
    		}
    		
            $info = $file->move(ROOT_PATH . 'public/static/uploads');
            if ($info) {
                $images[] = '/'.$info->getSaveName();
            }

	        $data['images'] = json_encode($images);
    	}

    	db('circle_goods_mood')->where(['cid'=>$cid])->update($data);
    }

    /**
    *	商品详情
    */
    public function circleGoodsInfo(){
    	$goods_id = $this->requestData['goods_id'];
    	$type = $this->requestData['circle_type'];
    	if(!in_array($type, ['1','2']))return ['message'=>'type参数有误!'];

    	if($type == '1'){
    		$circle_goods = db('circle_goods');
    	}else{
    		$circle_goods = db('circle_goods_demand');
    	}

    	$circle_goods = $circle_goods->where(['id'=>$goods_id])->find();

    	if(empty($circle_goods))return ['message'=>'circle_type或goods_id参数有误!'];
    	if($type == '1'){
    		$circle_goods['price'] = $circle_goods['price']/100;
    	}

    	if(empty($circle_goods))return ['message'=>'商品不存在!'];

    	$circle_goods['image'] = IMG_PATH.$circle_goods['image'];
    	if(!empty($circle_goods['images'])){
    		$circle_goods['images'] = json_decode($circle_goods['images'],1);
    		foreach ($circle_goods['images'] as $key => &$value) {
    			$value = IMG_PATH.$value;
    		}
    	}else{
    		$circle_goods['images'] = [];
    	}

    	//是否已收藏
    	$circle_goods_keep = db('circle_goods_keep')->where(['cid'=>$circle_goods['cid'],'goods_id'=>$goods_id,'user_id'=>$this->user['id']])->find();

    	if(!empty($circle_goods_keep)){
    		$circle_goods['is_goods_keep'] = '1';
    	}else{
    		$circle_goods['is_goods_keep'] = '0';
    	}

    	$circle_goods['remarks'] = db('circle')->where(['id'=>$circle_goods['cid']])->value('remarks');

		$cuid = db('circle')->where('id='.$circle_goods['cid'])->value('user_id');
		$hx = db('member')->where('id='.$cuid)->field('hx_username,hx_passwd')->find();
		$circle_goods['hx_username'] = $hx['hx_username'];
		$circle_goods['hx_passwd'] = $hx['hx_passwd'];

    	//查询用户
    	$circle_goods['member'] = db('member')->alias('m')->join('circle c','c.user_id=m.id')->where(['c.id'=>$circle_goods['cid']])->field('m.id,m.nickname,m.headimage,m.hx_username,m.hx_passwd,m.im_username')->find();

    	//检查是否有删除评论全新啊
    	if($circle_goods['member']['id'] == $this->user['id']){
    		$circle_goods['is_common_del'] = '1';
    	}else{
    		$circle_goods['is_common_del'] = '0';
    	}

    	return $circle_goods;
    }

    /**
    *	商品评论
    */
    public function goodsReview(){
    	$goods_id = $this->requestData['goods_id'];
    	$cid = $this->requestData['cid'];

    	$page = isset($this->requestData['page'])?$this->requestData['page']:'1';

    	$order_review = db('order_review')->where(['goods_id'=>$goods_id,'cid'=>$cid])->page($page,'10')->select();

    	return $order_review;
    }

    /**
    *   商品评论删除
    */
    public function goodsReviewDel(){
    	$id = $this->requestData['id'];
    	$user_id = db('order_review')->alias('or')->join('circle c','c.id = or.cid')->where(['or.id'=>$id])->value('c.user_id');
    	if(empty($user_id))return ['message'=>'id非法参数!'];
    	if($user_id != $this->user['id'])return ['message'=>'没有权利删除评论!'];
    	$result = db('order_review')->where(['id'=>$id])->delete();
    	if(!$result)return ['message'=>'删除失败!'];
    }

    /**
    * 圈商品图片上传
    */
    public function editGoodsImg(){
    	$goods_id = $this->requestData['goods_id'];
    	$circle_type = $this->requestData['circle_type'];
    	$type = $this->requestData['type'];

    	if(!in_array($circle_type,['1','2']))return ['message'=>'circle_type参数有误!'];
    	if(!in_array($type,['1','2']))return ['message'=>'type参数有误!'];

    	if($circle_type == '1'){
    		$circle_goods = db('circle_goods');
    	}

    	if($circle_type == '2'){
    		$circle_goods = db('circle_goods_demand');
    	}

    	$circle_goods_result = $circle_goods->where(['id'=>$goods_id])->field('images')->find();

    	if(empty($circle_goods_result))return ['message'=>'goods_id参数有误!'];

    	$file = request()->file('image');
    	$data = [];
    	if($type == '1'){
    		//上传主图
	        if ($file) {
	            $info = $file->move(ROOT_PATH . 'public/static/uploads');
	            if ($info) {
	                $data['image'] = '/'.$info->getSaveName();
	            }
	        }
    	}

    	if($type == '2'){
    		//上传多图
    		$images = [];
    		if(!empty($circle_goods_result['images'])){
    			$images = json_decode($circle_goods_result['images'],1);
    		}
    		
	        if ($file) {
	            $info = $file->move(ROOT_PATH . 'public/static/uploads');
	            if ($info) {
	                $images[] = '/'.$info->getSaveName();
	            }
	        }

	        $data['images'] = json_encode($images);
    	}
    	
    	$result = $circle_goods->where(['id'=>$goods_id])->update($data);
    	if(!$result)return ['message'=>'上传失败'];
    }

    /**
    *   商品图片删除
    */
    public function delGoodsImg(){
    	$goods_id = $this->requestData['goods_id'];
    	$circle_type = $this->requestData['circle_type'];
    	$img_path = $this->requestData['img_path'];

    	if(!in_array($circle_type,['1','2']))return ['message'=>'circle_type参数有误!'];

    	if($circle_type == '1'){
    		$circle_goods = db('circle_goods');
    	}

    	if($circle_type == '2'){
    		$circle_goods = db('circle_goods_demand');
    	}

    	$circle_goods_result = $circle_goods->where(['id'=>$goods_id])->field('images,cid')->find();

    	if(empty($circle_goods_result))return ['message'=>'goods_id参数有误!'];
    	$circle = db('circle')->where(['id'=>$circle_goods_result['cid']])->field('user_id')->find();
    	if($circle['user_id'] != $this->user['id'])return ['message'=>'只能修改自己发布的商品!'];

    	$images_array = @json_decode($circle_goods_result['images'],1);

    	$key = array_search($img_path, $images_array);
    	
    	if(!is_numeric($key))return ['message'=>'img_path参数有误!'];

    	unset($images_array[$key]);
    	
    	$images_array = array_values($images_array);

    	$result = $circle_goods->where(['id'=>$goods_id])->update(['images'=>json_encode($images_array)]);

    	if(!$result)return ['message'=>'修改失败!'];
    }

}